//
//  TopicListViewViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/4/7.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "TopicListViewViewController.h"

@interface TopicListViewViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@end

@implementation TopicListViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIFont systemFontOfSize:15],UITextAttributeFont,nil]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSFontAttributeName:[UIFont systemFontOfSize:15],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]
                                                                      }];
    [self createAf];
    
    [self createTableView];
    [self createRefreshView];
    [self downLoadData:self.topicListUrl];
}


- (void)downLoadData:(NSString *)url{
    [self downLoadDataWithUrl:[NSString stringWithFormat:self.topicListUrl,self.currentPage]];
}
- (void)navigationItemSetting{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[[UIImage imageNamed: @"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createAf{
    self.manager = [AFHTTPRequestOperationManager manager];
    self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    self.dataArray = [[NSMutableArray alloc] init];
}

#pragma mark - 创建刷新以及下拉
- (void)createRefreshView  {
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing = YES;
        weakSelf.currentPage = 0;
        NSString *url = [NSString stringWithFormat:weakSelf.topicListUrl,self.currentPage * 10];
        [weakSelf downLoadDataWithUrl:url];
    }];
    
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore = YES;
        weakSelf.currentPage ++;
        NSString *url = [NSString stringWithFormat:weakSelf.topicListUrl,self.currentPage * 10];
        [weakSelf downLoadDataWithUrl:url];
    }];
}

#pragma mark - 结束刷新
- (void)endRefreshing {
    if (self.isRefreshing) {
        self.isRefreshing = NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultNone];
    }
    if (self.isLoadMore) {
        self.isLoadMore = NO;
        [self.tableView footerEndRefreshing];
    }
}

#pragma mark - tableView创建
- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64-49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor blackColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"LeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"LeaderCell"];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - 下载数据
- (void)downLoadDataWithUrl:(NSString *)url{
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleShrink];
    [MMProgressHUD showWithTitle:@"请稍后" status:@"Loading..."];
    
    __weak typeof(self)weakSelf = self;
    [weakSelf.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSLog(@"专题列表网络下载");
            if (weakSelf.currentPage == 0) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *dataArr = jsonDict[@"data"];
            for (NSDictionary *topicDict in dataArr) {
                TopicModel *topicModel = [[TopicModel alloc] init];
                [topicModel setValuesForKeysWithDictionary:topicDict];
                
                NSDictionary *diantaiDict = topicDict[@"diantai"];
                [topicModel.dianTaiModel setValuesForKeysWithDictionary:diantaiDict];
                
                NSDictionary *userDict = diantaiDict[@"user"];
                [topicModel.dianTaiModel.userModel setValuesForKeysWithDictionary:userDict];
                [self.dataArray addObject:topicModel];
            }
        }
        [weakSelf endRefreshing];
        [self.tableView reloadData];
        [MMProgressHUD dismissWithSuccess:@"下载完成" title:@"ok"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"首页下载失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeaderCell"];
    TopicModel *model = self.dataArray[indexPath.row];
    cell.TitleLabe.text = model.title;
    cell.NameLabel.text = model.speak;
    [cell.CoverImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed: @"placehold_rec"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicModel *model = self.dataArray[indexPath.row];
    
    LeaderTableViewCell *cell = (LeaderTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGFloat y = cell.frame.origin.y - tableView.contentOffset.y;
    [LZXHelper animationPlayWithUrl:model.cover viewController:self leaderFrame:CGRectMake(8, y+64+7, 50, 50) newFrame:CGRectMake(self.view.bounds.size.width-30, 30, 6, 6)];
    
    [PlayerViewController sharePlayer].mainImageUrl = model.cover;
    [PlayerViewController sharePlayer].topicTitle = model.title;
    [PlayerViewController sharePlayer].viewNum = model.viewnum;
    [PlayerViewController sharePlayer].avaImageUrl = model.dianTaiModel.userModel.avatar;
    [PlayerViewController sharePlayer].speakNickName = model.dianTaiModel.userModel.nickname;
    
    [[PlayerViewController sharePlayer] playWithUrl:model.url];
}




@end
